// GlobalViewModelFactory.java
package com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.utils;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.example.ai_basedintelligentdiagnosisandtreatmentofchildren.model.UserViewModel;

public class GlobalViewModelFactory implements ViewModelProvider.Factory {
    private static GlobalViewModelFactory instance;
    private final Application application;

    private GlobalViewModelFactory(Application application) {
        this.application = application;
    }

    public static synchronized GlobalViewModelFactory getInstance(Application application) {
        if (instance == null) {
            instance = new GlobalViewModelFactory(application);
        }
        return instance;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(UserViewModel.class)) {
            // 传递 application 到构造函数
            return (T) new UserViewModel(application);
        }
        throw new IllegalArgumentException("Unknown ViewModel class");
    }
}